/**
 * 
 *      Code contributed by IUP Research Institute
 * 
 *     This file is part of NetworkMon.
 *
 *   NetworkMon is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; version 2 of the License.
 *
 *   NetworkMon is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with NetworkMon; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package networkgui;

/**
 * 
 * @author Brian Slocum
 */

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import networkbackend.AddressLinkedList;
import networkbackend.AddressNode;

public class ComputerNode extends javax.swing.JPanel {
	protected javax.swing.event.EventListenerList listenerList = new javax.swing.event.EventListenerList();
	private JTable theComputers;
	private ComputersTableModel theComputerModel;

	// private theComputerList;

	/** Creates a new instance of ComptuerNode */
	public ComputerNode() {

		initControls();
	}

	private void initControls() {
		// set up the computer information table
		theComputers = new JTable();
		theComputerModel = new ComputersTableModel();
		theComputers = new JTable(theComputerModel);

		// add the moust listner to the table
		theComputers.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					Point p = e.getPoint();
					int row = theComputers.rowAtPoint(p); // this is the views
															// row sorting will
															// throw this off

					// alert computer information that it needs to display this
					// data
					fireTheEvent(row);
				}
			}
		});

		// set up the frames stuffs
		setBorder(javax.swing.BorderFactory.createEtchedBorder());
		setLayout(new GridLayout(0, 1));
		setBackground(Color.WHITE);

		add(new JScrollPane(theComputers)); // this right here is bull sh*t...
											// it is the only way to the the
											// frecken headers to show up
	}

	public void updateComputerList(AddressLinkedList theComputerArray) {
		theComputerModel.setInformation(theComputerArray);
	}

	// this is when someone clicks on a computer node
	public void addListener(ANodeWasClickedEvent listener) {
		listenerList.add(ANodeWasClickedEvent.class, listener);
	}

	public void removeListener(ANodeWasClickedEvent listener) {
		listenerList.remove(ANodeWasClickedEvent.class, listener);
	}

	void fireTheEvent(int evt) {
		Object[] listeners = listenerList.getListenerList();
		// Each listener occupies two elements - the first is the listener class
		// and the second is the listener instance
		for (int i = 0; i < listeners.length; i += 2) {
			if (listeners[i] == ANodeWasClickedEvent.class) {
				((ANodeWasClickedEvent) listeners[i + 1])
						.ANodeWasClickedEvent(evt);
			}
		}
	}

	public static class ComputersTableModel extends AbstractTableModel {

		// java.util.List <AddressNode> myComputerList ;
		AddressLinkedList myComputerList = new AddressLinkedList();

		private String[] theHeadings = new String[] { "Host", "Sent/Sec",
				"Rec/Sec", };

		public int getRowCount() {
			return myComputerList.size();
		}

		public int getColumnCount() {
			return theHeadings.length;
		}

		@Override
		public String getColumnName(int column) {
			return theHeadings[column];
		}

		public Object getValueAt(int row, int column) {
			/*
			 * col 0 = Host col 1 = Sent/Sec col 2 = Rec/Sec
			 */

			switch (column) {
			case 0:
				return ((AddressNode) myComputerList.get(row)).address;
			case 1:
				return ((AddressNode) myComputerList.get(row)).totalSentNumberOfBytes;
			case 2:
				return ((AddressNode) myComputerList.get(row)).totalReceivedNumberOfBytes;
			default:
				return ((AddressNode) myComputerList.get(row)).address;
			}
		}

		public void setInformation(AddressLinkedList TheComputerArray) {
			int tempCounter = 0;
			myComputerList = TheComputerArray;
			fireTableDataChanged();
		}

	}
}
